﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter16
{
    using System.Windows.Forms;
    using System.Drawing;
    using System.IO;

    partial class Program
    {
        internal static void Begin(Action action, bool active = false)
        {
            if (active)
            {
                Console.WriteLine("开始演示：");
                action();
                Console.WriteLine("演示完毕：");
                Console.ReadLine();
            }
        }

        internal static void C01()
        {
            {
                int[] myIntegers = new int[100];
            }
            {
                Control[] myComtrol = new Control[50];
                myComtrol[1] = new Button();
                myComtrol[2] = new TextBox();
                myComtrol[3] = myComtrol[2];
                myComtrol[46] = new DataGrid();
                myComtrol[48] = new ComboBox();
                myComtrol[49] = new Button();
            }
            {
                double[,] myDouble = new double[10, 20];
                string[, ,] myStrings = new string[5, 3, 10];
                Point[][] myPolygons = new Point[3][];
                myPolygons[0] = new Point[10];
                myPolygons[1] = new Point[20];
                myPolygons[2] = new Point[30];
                for (int i = 0; i < myPolygons[0].Length; i++)
                {
                    Console.WriteLine(myPolygons[0][i]);
                }
            }
            {
                string[] names = new string[] { "Aidan", "Grant" };
            }
            {
                var names = new string[] { "Aidan", "Grant" };
            }
            {
                var names = new[] { "Aidan", "Grant", null };
            }
            {
                string[] names = { "Aidan", "Grant" };
            }
            {
                var kids = new[] { new { Name = "Aidan" }, new { Name = "Grant" } };
                foreach (var kid in kids)
                {
                    Console.WriteLine(kid.Name);
                }
            }
        }

        internal static void C02()
        {
            {
                FileStream[,] fs2dim = new FileStream[5, 10];
                object[,] o2dim = fs2dim;
                Stream[,] s2dim = (Stream[,])o2dim;
                int[] ildim = new int[5];
                object[] obldim = new object[ildim.Length];
                Array.Copy(ildim, obldim, ildim.Length);
            }
            {
                MyValueType[] src = new MyValueType[100];
                IComparable[] dest = new IComparable[src.Length];
                Array.Copy(src, dest, src.Length);
            }
            {
                string[] aa = new string[100];
                object[] oa = aa;
                oa[5] = "Jeff";
            }
        }

        private struct MyValueType : IComparable
        {
            public int CompareTo(object obj)
            {
                return 0;
            }
        }

        internal static void C06()
        {
            int[] lowerBounds = { 2005, 1 };
            int[] lengths = { 5, 4 };
            decimal[,] quarterlyRevenue = (decimal[,])Array.CreateInstance(typeof(decimal), lengths, lowerBounds);
            Console.WriteLine("{0,4} {1,9} {2,9} {3,9} {4,9}", "Year", "Q1", "Q2", "Q3", "Q4");
            int firstYear = quarterlyRevenue.GetLowerBound(0);
            int lastYear = quarterlyRevenue.GetUpperBound(0);
            int firstQuarter = quarterlyRevenue.GetLowerBound(1);
            int lastQuarter = quarterlyRevenue.GetUpperBound(1);
            for (int year = firstYear; year <= lastYear; year++)
            {
                Console.Write(year + " ");
                for (int quarter = firstQuarter; quarter <= lastQuarter; quarter++)
                {
                    Console.Write("{0,9:C} ", quarterlyRevenue[year, quarter]);
                }
                Console.WriteLine();
            }
        }

        internal static void C07()
        {
            {
                Array a;
                a = new string[0];
                Console.WriteLine(a.GetType());
                a = Array.CreateInstance(typeof(string), new int[] { 0 }, new int[] { 1 });
                Console.WriteLine(a.GetType());
                Console.WriteLine();
                a = new string[0, 0];
                Console.WriteLine(a.GetType());
                a = Array.CreateInstance(typeof(string), new int[] { 0, 0 }, new int[] { 0, 0 });
                Console.WriteLine(a.GetType());
                a = Array.CreateInstance(typeof(string), new int[] { 0, 0 }, new int[] { 1, 1 });
                Console.WriteLine(a.GetType());
            }
            {
                int[] a = new int[5];
                for (int i = 0; i < a.Length; i++)
                {
                    Console.WriteLine(a[i]);
                }
            }
        }
    }
}
